<?php



function live_chat($args = null) {
	
	$channel = 0;
	$limit = 8;
	$original_limit = $limit;
	$show_user = TRUE;
	$user_field = 'user_username';
	$profile_link = FALSE;
	parse_str($args);
	
	
	$log = CN_DOCUMENT_ROOT . 'modules/live-chat/log_'.$channel.'.txt';
	$fh = fopen($log, 'c') or die("can't open file");
			
	
	$contents = file_get_contents($log);
	if($contents) $contents = explode('*!*!', $contents);
	else $contents = array();
	$count = count($contents) - 1;
	$chats = array();
	if($count < $limit) {
		$limit = $count;
		$start = 1;
	}
	else {
		$start = $count - $limit + 1;
	}
	if($count) {
		for($i = $start; $i<=$count; $i++) {
			$chat = $contents[$i];
			$chats[] = '<li>'.substr($chat, 23).'</li>';
		} 
	}
	?>
	<ul id="live-chat" data-user-field="<?=$user_field?>" data-show-user="<?=$show_user?>" data-profile-link="<?=$profile_link?>" data-channel="<?=$channel?>" data-limit="<?=$original_limit?>" data-last="<?=$count?>">
	<?php
	if($chats) {
		foreach($chats as $chat) {
			echo $chat;
		}
	}
	?></ul>
	<input type="text" class="live-chat-input" id="live-chat-input"/> <input type="button" class="live-chat-send" id="live-chat-send" value="Send" data-channel="<?=$channel?>"/>
	<?php
	
}

?>